import {
  cusAddDocsQuery,
  querySalesman,
  addCustomer,
  updateCustomer
} from "@/api/cusAddDocs";

function getDetail() {
  let detail = {};
  try {
    detail = JSON.parse(sessionStorage.getItem("cusDocDetail"));
  } catch (error) {
    return undefined;
  }
  return detail;
}

const state = {
  docsDetail: getDetail() || {}
};
const actions = {
  cusAddDocsQuery({}, payload) {
    return new Promise((resolve, reject) => {
      cusAddDocsQuery(payload)
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });
  },
  querySalesman({}, payload) {
    return new Promise((resolve, reject) => {
      querySalesman(payload)
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });
  },
  addCustomer({}, payload) {
    return new Promise((resolve, reject) => {
      addCustomer(payload)
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });
  },
  updateCustomer({}, payload) {
    return new Promise((resolve, reject) => {
      updateCustomer(payload)
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });
  }
};
const mutations = {
  setDocsDetail(state, payload) {
    sessionStorage.setItem("cusDocDetail", JSON.stringify(payload));
    state.docsDetail = payload;
  },
  resetDocsDetail(state, payload) {
    sessionStorage.removeItem("cusDocDetail");
    state.docsDetail = {};
  }
};
const getters = {};
export default {
  namespaced: true,
  state,
  actions,
  mutations,
  getters
};
